package com.leo.base.exception.handler;

import com.leo.base.exception.CustomException;
import com.leo.base.exception.result.R;
import com.leo.base.exception.result.impl.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName ControllerExceptionHandler
 * @Description 自定义全局异常处理
 * @Author wangss
 * @date 2020.09.16 20:53
 * @Version 1.0
 */
@Slf4j
@ControllerAdvice
public class ControllerExceptionHandler {

    /**
     * 自定义异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public R error(CustomException e) {
        log.error(e.getMessage());
        return R.setResult(ResultCodeEnum.CUSTOM_ERROR).message(e.getMessage()).code(e.getCode());
    }

    /**
     * 空指针异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public R error(NullPointerException e) {
        log.error(e.getMessage());
        return R.setResult(ResultCodeEnum.NULL_POINT);
    }

    /**
     * 通用异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e) {
        log.error(e.getMessage());
        return R.error();
    }

}
